package com.tongji.housediy.view.components
{
	import com.tongji.housediy.utils.CollisionDetection;
	import com.tongji.housediy.utils.ConstVariableInventory;
	import com.tongji.housediy.utils.MathCalculation;
	import com.tongji.housediy.vo.Goods;
	import com.tongji.housediy.vo.PicModel;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	
	/**
	 * 这个类是绘制形状（点、线、面）的基类
	 * @author chenchao
	 * 
	 */	
	public class RoomShape extends UIComponent implements ICommonOperater
	{
		public var data:Vector.<Number>;
		public var index:int;
		
		// 保存每一帧的移动偏移量
		protected var offsetX:Number;
		protected var offsetY:Number;
		
		private var tempX:Number;
		private var tempY:Number;
		
		// 供 Furniture 类和 AbstractBorderChild 类调用
		public var selectedColorID:int;
		public var selectedSizeID:int;
		public var hostGoods:Goods;
		
		public function RoomShape(data:Vector.<Number> = null, index:int = -1)
		{
			if (data != null)
				this.data = data;
			if (index != -1)
				this.index = index;
			
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		}
		
		/**
		 * 仅供 Furniture 类和 AbstractBorderChild 类使用的函数
		 * 用于获取对象包围盒中心点相对于 GridContainer 的 Rectangle
		 * @return 返回对象包围盒中心点相对于 GridContainer 的 Rectangle
		 * 
		 */		
		public function updateBoundingAndCenterPoint():Rectangle
		{
			return this.getRect(this.parent.parent.parent);
		}
		
		/**
		 * 仅供 Furniture 类和 AbstractBorderChild 类使用的函数
		 * 用于改变大小和颜色
		 * @param picModel
		 * 
		 */		
		public function changeSizeAndColor(picModel:PicModel, bitmapData:BitmapData = null, hostGoods:Goods = null):void
		{
		}
		
		/**
		 * 重新设置绘制的 顶点数组，并且重绘
		 * @param data 绘制所需的 顶点数组
		 * 
		 */		
		public function resetData(offsetX:Number, offsetY:Number):void
		{
			for (var i:int = 0; i < data.length; i++)
			{
				if (i % 2 == 0) data[i] += offsetX;
				else data[i] += offsetY;
			}
			draw();
		}
		
		protected function draw():void
		{
			
		}
		
		protected function mouseOverHandler(event:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}
		
		protected function mouseMoveHandler(event:MouseEvent):void
		{
			
		}
		
		protected function mouseOutHandler(event:MouseEvent):void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}
		
		protected function mouseDownHandler(event:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			this.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			this.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			
			tempX = stage.mouseX;
			tempY = stage.mouseY;
			
			event.stopPropagation();
		}
		
		protected function enterFrameHandler(event:Event):void
		{
			var multipler:Number = 1 / MathCalculation.getZoomMultiple();
					
			offsetX = (stage.mouseX - tempX) * multipler;
			offsetY = (stage.mouseY - tempY) * multipler;
			
			tempX = stage.mouseX;
			tempY = stage.mouseY;
		}
		
		protected function mouseUpHandler(event:MouseEvent):void
		{
			this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		}
		
		public function moveShape(xOffset:Number = 0, yOffset:Number = 0):Boolean
		{
			return true;
		}
	}
}